<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="stuNo">
        <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="房间" prop="roomId">
        <el-input v-model="queryParams.roomId" placeholder="请输入房间id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker clearable v-model="queryParams.startDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker clearable v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="应收费用" prop="needMoney">
        <el-input v-model="queryParams.needMoney" placeholder="请输入应收费用" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="农行卡号" prop="bankCard">
        <el-input v-model="queryParams.bankCard" placeholder="请输入农行卡号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学年" prop="stuYearId">
        <el-select v-model="queryParams.stuYearId" clearable>
          <el-option v-for="(v, i) in year_list" :key="i" :label="v.stuYearName" :value="v.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生电话" prop="stuPhone">
        <el-input v-model="queryParams.stuPhone" placeholder="请输入学生电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
          @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
          @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="newRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" align="center" prop="id" />
      <el-table-column label="学号" align="center" prop="stuNo" />
      <el-table-column label="姓名" align="center" prop="stuName" />
      <el-table-column label="宿舍" align="center" prop="roomId" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.campusName + " " + scope.row.parkName + " " + scope.row.buildingName + " " +
      scope.row.floorName + "层" + scope.row.roomNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始日期" align="center" prop="startDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折合月数" align="center" prop="inMonth" />
      <el-table-column label="收费标准/10个月" align="center" prop="mainMoney" />
      <el-table-column label="应收费用" align="center" prop="needMoney" />
      <el-table-column label="农行卡号" align="center" prop="bankCard" />
      <el-table-column label="学年" align="center" prop="stuYearName" />
      <el-table-column label="费用状态" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_record_status" :value="scope.row.applyStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改住宿记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="学号" prop="stuNo">
          <el-input v-model="form.stuNo" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="房间id" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房间id" />
        </el-form-item>
        <el-form-item label="开始日期" prop="startDate">
          <el-date-picker clearable v-model="form.startDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endDate">
          <el-date-picker clearable v-model="form.endDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="折合月数" prop="inMonth">
          <el-input v-model="form.inMonth" placeholder="请输入折合月数" />
        </el-form-item>
        <el-form-item label="收费标准/10个月" prop="mainMoney">
          <el-input v-model="form.mainMoney" placeholder="请输入收费标准/10个月" />
        </el-form-item>
        <el-form-item label="应收费用" prop="needMoney">
          <el-input v-model="form.needMoney" placeholder="请输入应收费用" />
        </el-form-item>
        <el-form-item label="农行卡号" prop="bankCard">
          <el-input v-model="form.bankCard" placeholder="请输入农行卡号" />
        </el-form-item>
        <el-form-item label="学年id" prop="stuYearId">
          <el-select v-model="form.stuYearId" clearable>
            <el-option v-for="(v, i) in year_list" :key="i" :label="v.stuYearName" :value="v.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学生电话" prop="stuPhone">
          <el-input v-model="form.stuPhone" placeholder="请输入学生电话" />
        </el-form-item>
        <el-form-item label="是否是宿舍长" prop="isHead">
          <el-select v-model="form.isHead">
            <el-option :label="'是'" :value="1"></el-option>
            <el-option :label="'否'" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请状态" prop="applyStatus">
          <el-radio-group v-model="form.applyStatus">
            <el-radio v-for="dict in dict.type.dms_record_status" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { exportJwc, listNewRecord, getNewRecord, delNewRecord, addNewRecord, updateNewRecord } from "@/api/dormitory/new/record";

import { listAllYear } from "@/api/stuCQS/basedata/year";

export default {
  name: "NewRecord",
  dicts: ['dms_record_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 住宿记录表格数据
      newRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuNo: null,
        roomId: null,
        startDate: null,
        endDate: null,
        inMonth: null,
        mainMoney: null,
        needMoney: null,
        bankCard: null,
        stuYearId: null,
        stuPhone: null,
        applyStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

      year_list: []
    };
  },
  created() {
    this.listAllYear();
    this.getList();
  },
  methods: {
    async listAllYear() {
      let res = await listAllYear();
      if (res.code == 200) {
        console.log(res)
        this.year_list = [...res.data];
      }
    },

    /** 查询住宿记录列表 */
    getList() {
      this.loading = true;
      listNewRecord(this.queryParams).then(response => {
        this.newRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuNo: null,
        roomId: null,
        startDate: null,
        endDate: null,
        inMonth: null,
        mainMoney: null,
        needMoney: null,
        bankCard: null,
        stuYearId: null,
        stuPhone: null,
        applyStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加住宿记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNewRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改住宿记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNewRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNewRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除住宿记录编号为"' + ids + '"的数据项？').then(function () {
        return delNewRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dormitory/newRecord/export', {
        ...this.queryParams
      }, `newRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
